package com.spark.lola.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.warehouse.ProductInfo;
import com.spark.lola.entity.warehouse.StoreInfo;
import com.spark.lola.mapper.StoreInfoMapper;
import com.spark.lola.service.IStoreInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.mybatis.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author dengyejun
 * @since 2020-06-10
 */
@Service
public class StoreInfoServiceImpl extends BaseServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Override
    public List<SelectMap> selectMap(String key) {
        QueryWrapper<StoreInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("id,name");
        queryWrapper.eq("used", CommonConstant.Y);

        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(o->o.eq("id",key).or().like("name",key));
        }
        List<StoreInfo> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps =new ArrayList<>();
        if(!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getId(), item.getName()));
            });
        }
        return selectMaps;
    }

    @Override
    public List<StoreInfo> getStores() {
        QueryWrapper<StoreInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("id,name");
        queryWrapper.eq("used", CommonConstant.Y);
        return baseMapper.selectList(queryWrapper);
    }
}
